package github.kawaiior.yuanchuxiuzhen.common.container;

import github.kawaiior.yuanchuxiuzhen.common.container.number.PelletFurnaceContainerItemNumber;
import github.kawaiior.yuanchuxiuzhen.common.item.fire.StrangeFireItem;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.HerbItem;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.PelletItem;
import github.kawaiior.yuanchuxiuzhen.common.tileentity.PelletFurnaceTileEntity;
import github.kawaiior.yuanchuxiuzhen.core.init.BlockInit;
import github.kawaiior.yuanchuxiuzhen.core.init.ContainerInit;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;

import java.util.Objects;

public class PelletFurnaceContainer extends Container {

    public final PelletFurnaceTileEntity tileEntity;
    private final IWorldPosCallable ableToInteract;

    private PelletFurnaceContainerItemNumber intArray;

    public PelletFurnaceContainer(final int id, final PlayerInventory playerInventory, final PelletFurnaceTileEntity entity, PelletFurnaceContainerItemNumber intArray) {
        super(ContainerInit.PELLET_FURNACE_CONTAINER.get(), id);

        this.tileEntity = entity;
        this.ableToInteract = IWorldPosCallable.of(Objects.requireNonNull(tileEntity.getWorld()),tileEntity.getPos());

        this.intArray = intArray;

        Inventory herbInventory = entity.getHerbInventory();

        this.addSlot(new Slot(herbInventory, 0, 25, 12));
        this.addSlot(new Slot(herbInventory, 1, 7, 30));
        this.addSlot(new Slot(herbInventory, 2, 43, 30));
        this.addSlot(new Slot(herbInventory, 3, 25, 48));

        this.addSlot(new Slot(entity.getPelletInventory(), 0, 25, 30));

        this.addSlot(new Slot(entity.getFireInventory(), 0, 70, 48));

        this.addSlot(new Slot(entity.getRefinePelletInventory(), 0, 133, 30));

        // add player inventory
        for(int i =0;i<9;i++){
            this.addSlot(new Slot(playerInventory,i,i*18+8,152-10));
            this.addSlot(new Slot(playerInventory,i+9,i*18+8,94-10));
            this.addSlot(new Slot(playerInventory,i+18,i*18+8,112-10));
            this.addSlot(new Slot(playerInventory,i+27,i*18+8,130-10));
        }
    }

    public PelletFurnaceContainer(int id, PlayerInventory playerInventory, PacketBuffer data, PelletFurnaceContainerItemNumber intArray) {
        this(id, playerInventory, getTileEntity(playerInventory, data), intArray);
    }

    public static PelletFurnaceTileEntity getTileEntity(PlayerInventory playerInventory, PacketBuffer data){
        Objects.requireNonNull(playerInventory,"玩家物品栏不能为空");
        Objects.requireNonNull(data,"数据包不能为空");

        BlockPos blockPos = data.readBlockPos();
        TileEntity tileEntity = playerInventory.player.world.getTileEntity(blockPos);

        if(tileEntity instanceof PelletFurnaceTileEntity){
            return (PelletFurnaceTileEntity) tileEntity;
        }
        throw new IllegalStateException("invalid tile entity");
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return isWithinUsableDistance(ableToInteract, playerIn, BlockInit.PELLET_FURNACE_BLOCK.get());
    }

    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        // 操作非GUI slot
        if (slotId > 6 && clickTypeIn != ClickType.QUICK_MOVE){
            return super.slotClick(slotId, dragType, clickTypeIn, player);
        }

        if (tileEntity.isWorking()){
            return ItemStack.EMPTY;
        }
        ItemStack stack = slotClickInnerFunc(slotId, dragType, clickTypeIn, player);
        // 检查是否可以working
        tileEntity.checkWorking();
        return stack;
    }

    private ItemStack slotClickInnerFunc(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        if (clickTypeIn == ClickType.PICKUP){
            return pickup(slotId, dragType, clickTypeIn, player);
        }else if (clickTypeIn == ClickType.QUICK_MOVE){
            return quickMove(slotId, player);
        }else if (clickTypeIn == ClickType.PICKUP_ALL){
            return ItemStack.EMPTY;
        }else if (clickTypeIn == ClickType.QUICK_CRAFT){
            return player.inventory.getItemStack();
        }

        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    private ItemStack pickup(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player){

        // 拿到玩家鼠标上的物品
        ItemStack playerInvStack = player.inventory.getItemStack();

        // 草药槽
        if (slotId < 4){
            if (playerInvStack.isEmpty()){
                return super.slotClick(slotId, dragType, clickTypeIn, player);
            }
            if (playerInvStack.getItem() instanceof HerbItem){
                return super.slotClick(slotId, dragType, clickTypeIn, player);
            }
            return ItemStack.EMPTY;
        }

        // 妖丹槽
        if (slotId == 4){
            if (playerInvStack.isEmpty()){
                return super.slotClick(slotId, dragType, clickTypeIn, player);
            }
            if (playerInvStack.getItem() instanceof PelletItem){
                return super.slotClick(slotId, dragType, clickTypeIn, player);
            }
            return ItemStack.EMPTY;
        }

        // 异火槽
        if (slotId == 5){
            if (playerInvStack.isEmpty()){
                return super.slotClick(slotId, dragType, clickTypeIn, player);
            }
            if (playerInvStack.getItem() instanceof StrangeFireItem){
                return super.slotClick(slotId, dragType, clickTypeIn, player);
            }
            return ItemStack.EMPTY;
        }

        // 产出槽
        if (playerInvStack.isEmpty()){
            return super.slotClick(slotId, dragType, clickTypeIn, player);
        }

        return ItemStack.EMPTY;
    }

    private ItemStack quickMove(int slotId, PlayerEntity player){
        return ItemStack.EMPTY;
    }

}
